package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.repository.ApAssociateWordsRepository;
import com.heima.search.service.IAssociateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AssociateServiceImpl implements IAssociateService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApAssociateWordsRepository repository;

    @Override
    public ResponseResult search(ApArticleSearchDto dto) {
        if (StringUtils.isEmpty(dto.getSearchWords())) {
            List<ApAssociateWords> list = new ArrayList<>();
            return ResponseResult.okResult(list);
        }
        // 根据用户输入的关键字查询联想词
        // 分页查询
        // 按照时间倒序排列
        // 查询MongoDB
        List<ApAssociateWords> list = repository.findTop5ByAssociateWordsContainsOrderByCreatedTimeDesc(dto.getSearchWords());
        return ResponseResult.okResult(list);
    }
}
